﻿
using System;
using System.Collections.Generic;
using System.Data;
using QLKS.Entity;
using System.Linq;

namespace QLKS.DAL
{
    public class ChiTietPhieuThuePhongDAO
    {
        private SqlDataProvider dataProvider;

        public ChiTietPhieuThuePhongDAO()
        {
            dataProvider = new SqlDataProvider();
        }

        public List<ChiTietPhieuThuePhong> GetByAll(ChiTietPhieuThuePhong obj)
        {
            List<ChiTietPhieuThuePhong> lstData = new List<ChiTietPhieuThuePhong>(); ;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_ChiTietPhieuThuePhong_GetByAll"
                    , new string[] { "MaPhieuThuePhong", "KhachHang" }
                    , new object[] { obj.MaPhieuThuePhong, obj.KhachHangID }
                );
                ChiTietPhieuThuePhong entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new ChiTietPhieuThuePhong();
                    entity.MaPhieuThuePhong = Convert.ToInt32(myrow["MaPhieuThuePhong"]);
                    entity.KhachHangID = Convert.ToInt32(myrow["KhachHang"]);
                    try
                    {
                        if (entity.KhachHangID != null && entity.KhachHangID != 0)
                        {
                            entity.KhachHang = ((new DMKhachHangDAO()).GetByAll(new DMKhachHang(entity.KhachHangID))).FirstOrDefault();
                        }
                        else
                        {
                            entity.KhachHang = null;
                        }
                    }
                    catch
                    {
                        entity.KhachHang = null;
                    }

                    lstData.Add(entity);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }
    }
}
